/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * UserPanel.java
 *
 * Created on May 3, 2011, 10:24:27 AM
 */

package cloudclient;

import cloudclient.controller.CloudClientController;
import cloudclient.controller.CloudClientRunnableListener;
import cloudclient.helper.Utilities;

/**
 *
 * @author TimotiusNC
 */
public class UserPanel extends javax.swing.JPanel implements CloudClientRunnableListener {
    /** Creates new form UserPanel */
    public UserPanel(CloudClientView parent) {
        initComponents();

		currFolderLabel.setText(CloudClientController.getInstance().getCurrFolder().getAbsolutePath());
		onRevUpdate(CloudClientController.getInstance().getRev());
		m_parent = parent;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        appTitle = new javax.swing.JLabel();
        startStopBtn = new javax.swing.JButton();
        logoutBtn = new javax.swing.JButton();
        currFolderLabel = new javax.swing.JLabel();
        currRevLabel = new javax.swing.JLabel();
        revComboBox = new javax.swing.JComboBox();
        gotoRevLabel = new javax.swing.JLabel();
        gotoRevBtn = new javax.swing.JButton();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(cloudclient.CloudClientApp.class).getContext().getResourceMap(UserPanel.class);
        appTitle.setFont(resourceMap.getFont("appTitle.font")); // NOI18N
        appTitle.setText(resourceMap.getString("appTitle.text")); // NOI18N
        appTitle.setName("appTitle"); // NOI18N

        startStopBtn.setText(resourceMap.getString("startStopBtn.text")); // NOI18N
        startStopBtn.setName("startStopBtn"); // NOI18N
        startStopBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startStopBtnActionPerformed(evt);
            }
        });

        logoutBtn.setText(resourceMap.getString("logoutBtn.text")); // NOI18N
        logoutBtn.setName("logoutBtn"); // NOI18N
        logoutBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                logoutBtnActionPerformed(evt);
            }
        });

        currFolderLabel.setText(resourceMap.getString("currFolderLabel.text")); // NOI18N
        currFolderLabel.setName("currFolderLabel"); // NOI18N

        currRevLabel.setText(resourceMap.getString("currRevLabel.text")); // NOI18N
        currRevLabel.setName("currRevLabel"); // NOI18N

        revComboBox.setName("revComboBox"); // NOI18N

        gotoRevLabel.setText(resourceMap.getString("gotoRevLabel.text")); // NOI18N
        gotoRevLabel.setName("gotoRevLabel"); // NOI18N

        gotoRevBtn.setText(resourceMap.getString("gotoRevBtn.text")); // NOI18N
        gotoRevBtn.setName("gotoRevBtn"); // NOI18N
        gotoRevBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                gotoRevBtnActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(appTitle)
                    .addComponent(currFolderLabel)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(startStopBtn)
                        .addGap(18, 18, 18)
                        .addComponent(logoutBtn))
                    .addComponent(currRevLabel)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(gotoRevLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(revComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(gotoRevBtn)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(12, 12, 12)
                .addComponent(appTitle)
                .addGap(16, 16, 16)
                .addComponent(currFolderLabel)
                .addGap(18, 18, 18)
                .addComponent(currRevLabel)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(gotoRevLabel)
                    .addComponent(revComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(gotoRevBtn))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(logoutBtn)
                    .addComponent(startStopBtn))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

	private void logoutBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_logoutBtnActionPerformed
		CloudClientController.getInstance().logout();
		
		//Goto LoginPanel
		Utilities.getInstance().goToPanel(m_parent.getFrame(), this, new LoginPanel(m_parent));
	}//GEN-LAST:event_logoutBtnActionPerformed

	private void startStopBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startStopBtnActionPerformed
		if(!CloudClientController.getInstance().isSynching()){
			CloudClientController.getInstance().startSync(this);
			startStopBtn.setText("Stop Sync");
			gotoRevBtn.setEnabled(false);
		}
		else {
			CloudClientController.getInstance().stopSync();
			startStopBtn.setText("Start Sync");
			gotoRevBtn.setEnabled(true);
		}
	}//GEN-LAST:event_startStopBtnActionPerformed

	private void gotoRevBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_gotoRevBtnActionPerformed
		int rev = revComboBox.getSelectedIndex() + 1;
		
		CloudClientController.getInstance().gotoLatestRevision(rev);
		currRevLabel.setText("Revision : " + String.valueOf(rev));
	}//GEN-LAST:event_gotoRevBtnActionPerformed

	public void onRevUpdate(int rev) {
		currRevLabel.setText("Revision : " + String.valueOf(rev));

		if(rev > revComboBox.getItemCount()) {
			revComboBox.removeAllItems();
			for(int i=1; i<=rev; ++i)
				revComboBox.addItem(i);
		}
	}

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel appTitle;
    private javax.swing.JLabel currFolderLabel;
    private javax.swing.JLabel currRevLabel;
    private javax.swing.JButton gotoRevBtn;
    private javax.swing.JLabel gotoRevLabel;
    private javax.swing.JButton logoutBtn;
    private javax.swing.JComboBox revComboBox;
    private javax.swing.JButton startStopBtn;
    // End of variables declaration//GEN-END:variables

	private CloudClientView		m_parent;
}
